<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <link href="./css/head.css" rel="stylesheet">
    <link href="css/base.css" rel="stylesheet">

    <link href="css/buy.css" rel="stylesheet">
</head>
<body>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script src="js/Utils.js"></script>
    <script src="js/head.js"></script>

    <script src="js/mock.js"></script>
    <script src="mock/buy.js"></script>
    <script src="mock/recharge.js"></script>
    
    <div class="head"></div>
    <div class="nav_head"></div>

    <div class="main"></div>


    <script>
        $('document').ready(function(){
            let user = hasLogin()

            if(!user){
                alert("请先登录")
                location.href = "login.html"
                return
            }

            let username = user.username
            let total_price
            let order_number

            let data = {
                username
            }

            $.ajax({
                data,
                url:'http://localhost/order.php',
                type:'POST',
                success:function(res){
                    if( typeof(res) == 'string')
                        res = JSON.parse(res)
                        total_price = res.data.total_price
                        order_number = res.data.order_number

                    if(res.code == 200){
                        render(res.data)
                    }else{
                        render(null)
                    }
                }
            })

            function render(data){
                
                let pet_list = data.pet_list

                if(!data){
                    $('.main').html(`
                        <h2>订单异常</h2>
                    `)
                    location.href = 'car.html'
                    return
                }

                $('.main').append(`
                    <h2 text-align:center;>支付订单</h2>
                `)

                $('.main').append(`
                    <table>
                        <thead>
                            <th></th>    
                            <th>宠物名称</th>
                            <th>宠物数量</th>
                            <th>宠物价格</th>
                        </thead>   
                        <tbody></tbody> 
                    </table>
                `)

                for(let i = 0; i < pet_list.length; i++){
                    $('.main tbody').append(`
                        <tr>
                            <td>
                                <img src="${pet_list[i].image}">
                            </td>   
                            <td>
                                ${pet_list[i].pet_name}
                            </td> 
                            <td>
                                ${pet_list[i].number}    
                            </td>
                            <td>${pet_list[i].price}</td>
                        </tr>
                    `)
                }

                $('.main').append(`
                    <div class="button" id="commit" style="margin-top:20px; width:50%;">提交订单</div>
                `)

                commitEvent()
            }


            function getAmount(){
                let data = {
                    username
                }

                return $.ajax({
                    data,
                    url: 'http://localhost/amount.php',
                    type: 'POST',
                    success: function(res){

                    }

                })
            }

            //提交订单
            function commit(){

                let data ={
                    username,
                    order_number
                }

                return $.ajax({
                    data,
                    url:'http://localhost/commit_order.php',
                    type: 'POST',
                    success: function(res){

                    }
                })
            }

            //提交订单事件
            function commitEvent(){
                
                
                $('#commit').click(function(){
                    
                    getAmount().then(val => {
                        if(typeof(val) == 'string'){
                            val = JSON.parse(val)
                        }

                        if(val.code == 200){
                            let amount = val.data.amount

                            return amount
                        }
                        else{
                            return null
                        }
                    }).then(res => {
                        if(res && res > total_price){
                            return true
                        }
                        console.log(res, total_price)

                        return false
                    }).then(res => {
                        if(res){
                            return commit()
                        }

                        else
                            return false
                    }).then(res => {
                        if(res == false){
                            alert("余额不足")
                            return
                        }

                        if(typeof(res) == 'string')
                            res = JSON.parse(res)

                        if(res.code == 200){
                            alert(res.msg)
                            location.href="orders.html"
                        }
                        else{
                            alert("提交订单异常")
                        }
                    })

                })

            }
        })
    </script>
</body>
</html>